package com.fwtai.controller.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.service.web.GoodsCodeService;
import com.fwtai.tool.ToolClient;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 商品防伪码控制层|路由层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @Email service@fwtai.com
 * @创建日期 2024-02-20 12:03:46
 * @官网 <url>http://www.fwtai.com</url>
*/
@RestController
@RequestMapping("/goodsCode")
public class GoodsCodeController{

  private final GoodsCodeService goodsCodeService;

  public GoodsCodeController(final GoodsCodeService goodsCodeService){
    this.goodsCodeService = goodsCodeService;
  }

  /**新增入库或下载文件*/
  @PreAuthorize("hasAuthority('goods_code_btn_add')")
  @PostMapping("/add")
  public void add(final String target,final HttpServletRequest request,final HttpServletResponse response){
    switch (target){
      case "1"://新增入库
        ToolClient.responseJson(goodsCodeService.add(request),response);
        break;
      case "2"://下载文件
        goodsCodeService.downFile(request,response);
        break;
      default:
        ToolClient.responseJson(ToolClient.jsonValidateField(),response);
        break;
    }
  }

  /**查询单独下载防伪码*/
  @PreAuthorize("hasAuthority('goods_code_row_down')")
  @GetMapping("/queryDown")
  public void queryDown(final Long kid,final String name,final HttpServletResponse response){
    goodsCodeService.queryDown(kid,name,response);
  }

  /**获取数据*/
  @PreAuthorize("hasAuthority('goods_code_btn_listData')")
  @GetMapping("/listData")
  public void listData(final HttpServletRequest request,final HttpServletResponse response){
    ToolClient.responseJson(goodsCodeService.listData(new PageFormData(request)),response);
  }

  /*获取商品名称下拉框列动态值选项的数据*/
  @PreAuthorize("hasAuthority('goods_code_btn_goodsList')")
  @GetMapping("/getGoodsList")
  public void getGoodsList(final HttpServletResponse response){
    ToolClient.responseJson(goodsCodeService.getGoodsList(),response);
  }
}